package com.ruoyi.ly.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.ly.domain.SpaceMeeting;
import com.ruoyi.ly.domain.dto.PageSpaceMeetingDTO;
import com.ruoyi.ly.domain.dto.SpaceMeetingAddDTO;
import com.ruoyi.ly.domain.dto.SpaceMeetingUpdateDTO;
import com.ruoyi.ly.domain.vo.PageSpaceMeetingVO;
import com.ruoyi.ly.domain.vo.SpaceMeetingVO;
import com.ruoyi.ly.mapper.SpaceMeetingMapper;
import com.ruoyi.ly.service.SpaceMeetingService;
import com.ruoyi.ly.utils.ExportData;
import com.ruoyi.ly.utils.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

@Service
public class SpaceMeetingServiceImpl implements SpaceMeetingService {

    @Autowired
    private SpaceMeetingMapper spaceMeetingMapper;
    @Autowired
    private ExportData exportData;

    @Override
    public void save(SpaceMeetingAddDTO spaceMeetingDTO) {
        SpaceMeeting spaceMeeting =new SpaceMeeting();
        BeanUtils.copyProperties(spaceMeetingDTO,spaceMeeting);
        spaceMeeting.setCtime(String.valueOf(LocalDate.now()));
        spaceMeetingMapper.insert(spaceMeeting);
    }

    @Override
    public PageResult pageQuery(PageSpaceMeetingDTO pageSpaceMeetingDTO) {
        PageHelper.startPage(pageSpaceMeetingDTO.getPage(),pageSpaceMeetingDTO.getPageSize());
        Page<PageSpaceMeetingVO> page = spaceMeetingMapper.pageQuery(pageSpaceMeetingDTO);
        long total = page.getTotal();
        List<PageSpaceMeetingVO> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void delete(Long floorId) {
        spaceMeetingMapper.delete(floorId);
    }

    @Override
    public void update(SpaceMeetingUpdateDTO spaceMeetingUpdateDto) {
        SpaceMeeting spaceMeeting =new SpaceMeeting();
        BeanUtils.copyProperties(spaceMeetingUpdateDto,spaceMeeting);
        spaceMeetingMapper.update(spaceMeeting);
    }

    @Override
    public SpaceMeetingVO selectById(Long floorId) {
        SpaceMeetingVO spaceMeetingVO = spaceMeetingMapper.selectById(floorId);
        return spaceMeetingVO;
    }

    @Override
    public void exportData(HttpServletResponse response) {

        List<PageSpaceMeetingVO> spaceMeetings = spaceMeetingMapper.selectAll();
        exportData.exportDataEasyExcel(response,spaceMeetings,PageSpaceMeetingVO.class,"功能场地信息");
    }
}
